﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Text;
using System.Text.RegularExpressions;

namespace Microsoft.Samples.Office.UnifiedCommunications.Gadget
{
    public class StringFunctions
    {
        public static string TrimGadgetHeaderDisplayName(string displayName)
        {
            int maxLength = Convert.ToInt32(ConfigurationManager.AppSettings["HeaderMaxDisplayNameLength"]);
            if (displayName.Length >= maxLength)
                return TrimDisplayName(displayName, maxLength);
            else
                return displayName;
        }

        public static string TrimGadgetPersonaDisplayName(string displayName)
        {
            int maxLength = Convert.ToInt32(ConfigurationManager.AppSettings["PersonaMaxDisplayNameLength"]);
            if (displayName.Length >= maxLength)
                return TrimDisplayName(displayName, maxLength);
            else
                return displayName;
        }

        private static string TrimDisplayName(string displayName, int maxLength)
        {
            return displayName.Remove(maxLength - 1) + "...";
        }

        public static bool IsPhoneNumber(string inputString)
        {
            bool isPhoneNumber = true;

            // The only valid non-numeric characters in a phone number are +, -, (, and )
            //  Remove occurrences of these characters from the input string and then 
            //  check if the remaining value is numeric.
            //
            inputString = inputString.Replace(" ", string.Empty);
            inputString = inputString.Replace("+", string.Empty);
            inputString = inputString.Replace("-", string.Empty);
            inputString = inputString.Replace("(", string.Empty);
            inputString = inputString.Replace(")", string.Empty);

            string regexInt = @"^\d+$";
            if (Regex.Match(inputString, regexInt).Success == false)
                isPhoneNumber = false; 

            return isPhoneNumber;

            // Communicator will ultimately decide if it can dial this phone number.
            // Can't anticipate what regular expressions for dialing numbers are 
            //  in place in an OCS deployment. 
        }
    }
}
